package com.ruoyi.mall.product.interfaces.facade;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.mall.product.domain.entity.PmsAttr;
import com.ruoyi.mall.product.domain.entity.PmsAttrGroup;
import com.ruoyi.mall.product.domain.vo.PmsAttrGroupAttrsVo;
import com.ruoyi.mall.product.domain.vo.PmsAttrGroupVo;
import com.ruoyi.mall.product.domain.vo.TreeSelect;
import com.ruoyi.mall.product.service.PmsAttrGroupAppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 属性分组Controller
 *
 * @author zhangqishu
 * @date 2022-05-25
 */
@RestController
@RequestMapping("/group")
public class PmsAttrGroupController extends BaseController {
    @Autowired
    private PmsAttrGroupAppService pmsAttrGroupAppService;

    /**
     * 查询属性分组列表
     */
    @RequiresPermissions("product:group:list")
    @GetMapping("/list")
    public TableDataInfo list(PmsAttrGroup pmsAttrGroup) {
        startPage();
        List<PmsAttrGroup> list = pmsAttrGroupAppService.selectPmsAttrGroupList(pmsAttrGroup);
        return getDataTable(list);
    }

    /**
     * 查询属性分组关联关系
     */
    @GetMapping("{attrGroupId}/attr/relation")
    public TableDataInfo relation(@PathVariable("attrGroupId") Long attrGroupId) {
        startPage();
        List<PmsAttr> list = pmsAttrGroupAppService.selectPmsAttrGroupRelation(attrGroupId);
        return getDataTable(list);
    }

    /**
     * 查询分类下的分组信息 和分组信息所关联的属性信息
     */
    @GetMapping("/{categoryId}/withAttr")
    public AjaxResult listAttrGroupAndAttrs(@PathVariable("categoryId") Long categoryId)
    {
        List<PmsAttrGroupAttrsVo> pmsCategoryList= pmsAttrGroupAppService.listWithAttr(categoryId);
        return AjaxResult.success(pmsCategoryList);
    }

    /**
     * 查询属性分组没有关联的
     */
    @GetMapping("{attrGroupId}/noAttr/relation")
    public TableDataInfo relationNot(PmsAttr pmsAttr,@PathVariable("attrGroupId") Long attrGroupId) {
        startPage();
        List<PmsAttr> list = pmsAttrGroupAppService.selectPmsAttrGroupRelationNot(pmsAttr,attrGroupId);
        return getDataTable(list);
    }

    /**
     * 导出属性分组列表
     */
    @RequiresPermissions("product:group:export")
    @Log(title = "属性分组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PmsAttrGroup pmsAttrGroup) {
        List<PmsAttrGroup> list = pmsAttrGroupAppService.selectPmsAttrGroupList(pmsAttrGroup);
        ExcelUtil<PmsAttrGroup> util = new ExcelUtil<>(PmsAttrGroup.class);
        util.exportExcel(response, list, "属性分组数据");
    }

    /**
     * 获取属性分组详细信息
     */
    @RequiresPermissions("product:group:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(pmsAttrGroupAppService.selectPmsAttrGroupById(id));
    }

    /**
     * 新增属性分组
     */
    @RequiresPermissions("product:group:add")
    @Log(title = "属性分组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmsAttrGroup pmsAttrGroup) {
        return toAjax(pmsAttrGroupAppService.insertPmsAttrGroup(pmsAttrGroup));
    }

    /**
     * 新增属性分组的关联关系
     */
    @Log(title = "属性分组", businessType = BusinessType.INSERT)
    @PostMapping("/attr/relation")
    public AjaxResult addRelation(@RequestBody List<PmsAttrGroupVo> pmsAttrGroupVos) {
        return toAjax(pmsAttrGroupAppService.insertPmsAttrGroupRelation(pmsAttrGroupVos));
    }

    /**
     * 修改属性分组
     */
    @RequiresPermissions("product:group:edit")
    @Log(title = "属性分组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmsAttrGroup pmsAttrGroup) {
        return toAjax(pmsAttrGroupAppService.updatePmsAttrGroup(pmsAttrGroup));
    }

    /**
     * 删除属性分组
     */
    @RequiresPermissions("product:group:remove")
    @Log(title = "属性分组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pmsAttrGroupAppService.deletePmsAttrGroupByIds(ids));
    }

    /**
     * 删除属性关联关系
     */
    @Log(title = "属性分组", businessType = BusinessType.DELETE)
    @PostMapping("/relation/delete")
    public AjaxResult removeRelation(@RequestBody PmsAttrGroupVo[] pmsAttrGroupVo) {
        return toAjax(pmsAttrGroupAppService.deletePmsAttrGroupRelation(pmsAttrGroupVo));
    }
}
